﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;
namespace NJL.WebUsers.AdminEnterprise
{
    public partial class EditEnterPriseService : BasePage
    {
        NJL.BLL.NJL_COMPANY_SERVICE bll = new BLL.NJL_COMPANY_SERVICE();
        NJL.Model.NJL_COMPANY_SERVICE model;
        NJL.BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        void bind()
        {
            NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
            NJL.Model.NJL_COMPANY modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);  //根据用户编号查询企业编号  
            List<NJL.Model.NJL_COMPANY_SERVICE> modelServiceList = bll.GetModelList("NJL_COMPANY_ID='" + modelCompany.ID + "' and ID='"+Request.QueryString["id"]+"'");   
            if(modelServiceList.Count==0)
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this,"请登录后再编辑","/Login.aspx");
                return;
            }
            DataSet ds = bllServiceType.GetList("");
            rblServiceType.DataSource = ds;
            rblServiceType.DataTextField = "TYPE_NAME";
            rblServiceType.DataValueField = "TYPE_CODE";
            rblServiceType.DataBind();
            model = bll.GetModel(Request.QueryString["id"].ToString());
            txtAttractionsTitle.Text = model.SERVICE_NAME;
            txtContent.Text = model.SERVICE_CONTENT;
            txtTag.Text = model.TAG;
   


            for (var i = 1; i <= 5; i++)
            {
                rblServiceLevel.Items.Add(new ListItem("" + i, "" + i)); //服务等级
            }
            rblServiceLevel.Items.FindByValue(model.SERVICE_LEVEL.ToString()).Selected = true;
            rblServiceType.Items.FindByValue(model.TYPE_CODE).Selected = true;
            if (model.IMAGE_CODE == "")
            {
                Image1.Visible = false;
            }
            else
            {
                Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
            }

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model = bll.GetModel(Request.QueryString["id"].ToString());
            model.SERVICE_NAME = PageValidate.InputText(txtAttractionsTitle.Text.Trim(),50);
            model.SERVICE_CONTENT = txtContent.Text.Trim();
            model.TAG = PageValidate.InputText(txtTag.Text.Trim(),100);
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.TYPE_CODE = rblServiceType.SelectedValue;
            model.SERVICE_LEVEL = Convert.ToInt32(rblServiceLevel.SelectedValue);
            if (bll.Update(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "EnterPriseService.aspx");
            }
        }
        protected void bntUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "上传成功", "EnterPriseService.aspx");
                        if (model.IMAGE_CODE != "")
                        {
                            Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                            Image1.Visible = true;
                        }
                        else
                        {
                            Image1.Visible = false;
                        }
                    }

                }
            }
        }
    }
}